<?php

/**
 * This is the model class for table "tbl_collection".
 *
 * The followings are the available columns in table 'tbl_collection':
 * @property string $id
 * @property string $name
 * @property integer $status
 * @property integer $like_cnt
 * @property integer $view_cnt
 * @property string $user_created
 * @property string $created_at
 * @property string $user_updated
 * @property string $updated_at
 *
 * The followings are the available model relations:
 * @property User $userCreated
 * @property User $userUpdated
 * @property Image[] $image
 */
class Collection extends CActiveRecord {

    const COLLECTION_LOCKED = 0;
    const COLLECTION_ACTIVE = 1;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Collection the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return 'tbl_collection';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules() {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name, user_created, created_at', 'required'),
            array('status, like_cnt, view_cnt', 'numerical', 'integerOnly' => true),
            array('name', 'length', 'max' => 100),
            array('user_created, user_updated', 'length', 'max' => 20),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name, status, user_created, created_at, user_updated, updated_at', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        return array(
            'userCreated' => array(self::BELONGS_TO, 'User', 'user_created'),
            'image' => array(self::MANY_MANY, 'Image', 'tbl_collection_image(collection_id, image_id)'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'name' => 'Name',
            'status' => 'Status',
            'user_created' => 'User Created',
            'created_at' => 'Created At',
            'user_updated' => 'User Updated',
            'updated_at' => 'Updated At',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search() {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id, true);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('status', $this->status, true);
        $criteria->compare('user_created', $this->user_created, true);
        $criteria->compare('created_at', $this->created_at, true);
        $criteria->compare('user_updated', $this->user_updated, true);
        $criteria->compare('updated_at', $this->updated_at, true);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

}